<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\User;

use App\Models\PointAccount;
use App\Models\PointReleaseRecord;
use App\Models\RewardTransaction;
use App\Models\User;
use App\Models\UserRewardAccount;
use App\Service\BaseService;
use App\Service\Notifications\NotificationService;

class RewardAccountService extends BaseService
{
    /**
     * 添加奖励余额.
     * @param $user_id
     * @param $amount
     * @param mixed $transaction_type
     * @param mixed $describe
     * @return bool|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     */
    public function recharge($user_id, $amount, $transaction_type = 1, $describe = '获取')
    {
        if ($amount <= 0) {
            return false;
        }
        $userRewardAccount = UserRewardAccount::query()->where('user_id', $user_id)->first();

        $userRewardAccount->total_balance = bcadd($userRewardAccount->total_balance, $amount, 2);
        $userRewardAccount->available_balance = bcadd($userRewardAccount->available_balance, $amount, 2);
        $userRewardAccount->save();
        if ($transaction_type == 1) {
            $this->releaseTheRecord($user_id, $amount, $userRewardAccount->available_balance);
        }
        $this->rewardChangeHistory($user_id, $amount, $userRewardAccount->available_balance, $transaction_type, 1, $describe);

        return $userRewardAccount;
    }

    /**
     * 减少奖励余额.
     * @param $user_id
     * @param $amount
     * @param mixed $transaction_type
     * @param mixed $describe
     * @return false|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed|string
     */
    public function consumption($user_id, $amount, $transaction_type = 1, $describe = '提现')
    {
        $userRewardAccount = UserRewardAccount::query()->where('user_id', $user_id)->first();

        if ($userRewardAccount->available_balance < $amount) {
            return false;
        }
        $userRewardAccount->available_balance = bcsub($userRewardAccount->available_balance, $amount, 2);
        $userRewardAccount->consumed_balance = bcadd($userRewardAccount->consumed_balance, $amount, 2);
        $userRewardAccount->save();
        if ($transaction_type == 1) {
            $this->releaseTheRecord($user_id, $amount, $userRewardAccount->available_balance);
        }
        $this->rewardChangeHistory($user_id, $amount, $userRewardAccount->available_balance, $transaction_type, 2, $describe);

        return $userRewardAccount;
    }

//    是否足够
    public function isItSufficient($user_id, $amount): bool
    {
        $userRewardAccount = UserRewardAccount::query()->where('user_id', $user_id)->first();
        if ($userRewardAccount->available_balance < $amount) {
            return false;
        }
        return true;
    }

    /**
     * 添加奖励余额释放获取日志.
     * @param $user_id
     * @param $amount
     * @param $remaining_points
     */
    private function releaseTheRecord($user_id, $amount, $remaining_points)
    {
        $point = PointAccount::query()->where('user_id', $user_id)
            ->first();
        PointReleaseRecord::query()->create([
            'user_id' => $user_id,
            'points_released' => $amount,
            'amount_released' => $remaining_points,
            'remaining_points' => $point->available_points,
            'exceeded_amount' => 0,
        ]);
//        $user = User::query()->find($user_id);
//        if (filled($user)) {
//            NotificationService::make()->SystemNotify($user, sprintf('释放积分:%s 到奖励账户', $amount * 100));
//        }
    }

    private function rewardChangeHistory($user_id, $amount, $balance, $transaction_type, $change_type, $describe, $status = 2)
    {
        RewardTransaction::query()->create([
            'user_id' => $user_id,
            'amount' => $amount,
            'balance' => $balance,
            'transaction_type' => $transaction_type,
            'change_type' => $change_type,
            'describe' => $describe,
            'status' => $status,
        ]);
//        $user = User::query()->find($user_id);
//        if (filled($user)) {
//            NotificationService::make()->SystemNotify($user, $describe);
//        }
    }
}
